/*
 *  This file is part of WWWPal.
 *
 *  Copyright (C) 2003  Dr. John Punin, Department of Computer Science
 *  Rensselaer Polytechnic Institute
 *
 *  WWWPal is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  WWWPal is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with Foobar; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

/*
 *  This file is part of WWWPal.
 *
 *  Copyright (C) 2003  Dr. John Punin, Department of Computer Science
 *  Rensselaer Polytechnic Institute
 *
 *  WWWPal is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  WWWPal is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with Foobar; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */
#include "LOGR_configuration.h"

extern void
delete_list_strings(LNode_type *llist);


OutputFileNodePtr
new_output_file_node(void)
{
  OutputFileNodePtr ofn = (OutputFileNodePtr)calloc(1, sizeof(OutputFileNodeStruct));
  if(!ofn) {
      fprintf(stderr,"Out of Memory new_output_file_node\n");
      abort();
  }
  return ofn;
}

void
delete_output_file_node(OutputFileNodePtr ofn)
{
    if(!ofn) return;

    if(ofn->lml_name) {
	free(ofn->lml_name); ofn->lml_name = NULL;
    }
    if(ofn->rep_prefix) {
	free(ofn->rep_prefix); ofn->rep_prefix = NULL;
    }
    if(ofn->rep_dir) {
	free(ofn->rep_dir); ofn->rep_dir = NULL;
    }
    free(ofn); ofn = NULL;
}

OutputFileNodePtr
get_i_output_file(LNode_type *lof, int i)
{
    LNode_type *ptr = NULL;
    if(!lof || i < 0)
	return NULL;
    
    for(ptr = lof->back; ptr != lof ; ptr=ptr->back)
    {
	OutputFileNodePtr ofn = (OutputFileNodePtr)ptr->info;
	if(ofn && ofn->i == i)
	    return ofn;
    }
    return NULL;
}

OutputFileNodePtr
LOGR_add_output_file(ConfigNodePtr cnp, int nelements,int nkeywords)
{
    OutputFileNodePtr ofn = NULL;
    if(!cnp)
	return NULL;
    if(nelements < 0)
	return NULL;
    ofn = new_output_file_node();
    ofn->i = cnp->nof;
    (cnp->nof)++;
    ofn->nele = nelements;
    ofn->nkey = nkeywords;
    Insert_linked_list(cnp->lout_files,create_new_node_llist((char *)ofn));
    return ofn;
}

void
LOGR_set_logmlfile_name(OutputFileNodePtr ofn, char *lml_name)
{
    if(!ofn || !lml_name)
	return;
    GRStrAllocCopy(ofn->lml_name,lml_name);
}

void
LOGR_set_report_prefix_name(OutputFileNodePtr ofn, char *rp)
{
    if(!ofn || !rp)
	return;

    GRStrAllocCopy(ofn->rep_prefix,rp);
}


void
LOGR_set_report_directory_name(OutputFileNodePtr ofn, char *dir)
{
    if(!ofn || !dir)
	return;

    GRStrAllocCopy(ofn->rep_dir,dir);
}

void
LOGR_set_user_session(OutputFileNodePtr ofn,int num_users, int num_graphs, 
		      int min_edges, int max_edges)
{
    if(!ofn)
	return;

    ofn->nus = num_users;
    ofn->ngraph = num_graphs;
    ofn->min_edges = min_edges;
    ofn->max_edges = max_edges;
}

ConfigNodePtr
new_config_node(void)
{
  ConfigNodePtr con = (ConfigNodePtr)calloc(1, sizeof(ConfigNodeStruct));
  if(!con) {
      fprintf(stderr,"Out of Memory new_config_node\n");
      abort();
  }

  con->lws_subnets = init_linked_list();
  con->lout_files = init_linked_list();

  return con;
}

void
delete_list_output_files(LNode_type *lof)
{
  LNode_type *ptr = NULL;

  if(!lof) return;

  for(ptr = lof->back ; ptr != lof ; ptr = ptr->back)
  {
      OutputFileNodePtr ofn = (OutputFileNodePtr)ptr->info;
      delete_output_file_node(ofn);
  }
  Delete_all_list(lof); 
}

void
delete_config_node(ConfigNodePtr con)
{	
    if(!con) return;

    if(con->ws_uri) {
	free(con->ws_uri); con->ws_uri = NULL;
    }
    if(con->ws_domain) {
	free(con->ws_domain); con->ws_domain = NULL;
    }
    if(con->lf_name) {
	free(con->lf_name); con->lf_name = NULL;
    }
    if(con->lf_prefix) {
	free(con->lf_prefix); con->lf_prefix = NULL;
    }
    if(con->lf_root_dir) {
	free(con->lf_root_dir); con->lf_root_dir = NULL;
    }
    if(con->gf_name) {
	free(con->gf_name); con->gf_name = NULL;
    }

    delete_list_strings(con->lws_subnets);
    delete_list_output_files(con->lout_files);

    free(con); con = NULL;
}

void
LOGR_set_config_webserver_uri(ConfigNodePtr con, char *wsu)
{
    if(!con) return;
    if(!wsu) return;
    GRStrAllocCopy(con->ws_uri,wsu);
}

char *
LOGR_get_config_webserver_uri(ConfigNodePtr con)
{
    if(!con) return NULL;
    return con->ws_uri;
}

void
LOGR_set_config_webserver_domain(ConfigNodePtr con, char *wsd)
{
    if(!con) return;
    if(!wsd) return;
    GRStrAllocCopy(con->ws_domain,wsd);
}

char *
LOGR_get_config_webserver_domain(ConfigNodePtr con)
{
    if(!con) return NULL;
    return con->ws_domain;
}


void
LOGR_set_config_logfile_name(ConfigNodePtr con, char *lfn)
{
    if(!con) return;
    if(!lfn) return;
    GRStrAllocCopy(con->lf_name,lfn);
}

char *
LOGR_get_config_logfile_name(ConfigNodePtr con)
{
    if(!con) return NULL;
    return con->lf_name;
}


void
LOGR_set_config_logfile_prefix(ConfigNodePtr con, char *lfp)
{
    if(!con) return;
    if(!lfp) return;
    GRStrAllocCopy(con->lf_prefix,lfp);
}

char *
LOGR_get_config_logfile_prefix(ConfigNodePtr con)
{
    if(!con) return NULL;
    return con->lf_prefix;
}

void
LOGR_set_config_logfile_uri_prefix(ConfigNodePtr con, char *lfup)
{
    if(!con) return;
    if(!lfup) return;
    GRStrAllocCopy(con->lf_uri_prefix,lfup);
}

char *
LOGR_get_config_logfile_uri_prefix(ConfigNodePtr con)
{
    if(!con) return NULL;
    return con->lf_uri_prefix;
}

void
LOGR_set_config_logfile_root_dir(ConfigNodePtr con, char *lfrd)
{
    if(!con) return;
    if(!lfrd) return;
    GRStrAllocCopy(con->lf_root_dir,lfrd);
}

char *
LOGR_get_config_logfile_root_dir(ConfigNodePtr con)
{
    if(!con) return NULL;
    return con->lf_root_dir;
}

void
LOGR_set_config_logfile_type(ConfigNodePtr con, short lft)
{
    if(!con) return;
    con->lf_type = lft;
}

short
LOGR_get_config_logfile_type(ConfigNodePtr con)
{    
    if(!con) return 0;
    return con->lf_type;
}

void
LOGR_set_config_logfile_res_ip(ConfigNodePtr con, short rhip)
{
    if(!con) return;
    con->lf_rhip = rhip;
}

short
LOGR_get_config_logfile_res_ip(ConfigNodePtr con)
{
    if(!con) return 0;
    return con->lf_rhip;

}

void
LOGR_set_config_logfile_res_ip_timeout(ConfigNodePtr con, int rhipt)
{
    if(!con) return;
    con->lf_rhipt = rhipt;
}

int
LOGR_get_config_logfile_res_ip_timeout(ConfigNodePtr con)
{
    if(!con) return 0;
    return con->lf_rhipt;
}

short 
LOGR_get_config_compute_user_sessions(ConfigNodePtr con)
{
    return con ? con->cus : -1;
}

void
LOGR_set_config_compute_user_sessions(ConfigNodePtr con)
{
    if(con)
	con->cus = 1;
}

void
LOGR_reset_config_compute_user_sessions(ConfigNodePtr con)
{
    if(con)
	con->cus = 0;
}

void
LOGR_set_config_graphfile_name(ConfigNodePtr con, char *gfn)
{
    if(!con) return;
    if(!gfn) return;
    GRStrAllocCopy(con->gf_name,gfn);
}

char *
LOGR_get_config_graphfile_name(ConfigNodePtr con)
{
    if(!con) return NULL;
    return con->gf_name;
}

LNode_type *
LOGR_get_config_list_subnet(ConfigNodePtr con)
{
    if(!con) return NULL;
    return con->lws_subnets;
}

int
LOGR_get_config_num_subnet(ConfigNodePtr con)
{
    if(!con) return 0;
    return con->nsn;
}

int
LOGR_get_config_num_output_files(ConfigNodePtr con)
{
    if(!con) return 0;
    return con->nof;
}

void
LOGR_add_config_subnet(ConfigNodePtr con, char *cidr)
{
    char *ncidr = NULL;
    if(!con) return;
    if(!cidr) return;
    GRStrAllocCopy(ncidr,cidr);
    (con->nsn)++;
    Insert_linked_list(con->lws_subnets,create_new_node_llist(ncidr));
}

int 
LOGR_get_config_nelements(ConfigNodePtr con,int i)
{
    OutputFileNodePtr ofn = NULL;
    if(!con || i <0)
	return -1;
    ofn = get_i_output_file(con->lout_files,i);

    return ofn ? ofn->nele : -1;
}

int 
LOGR_get_config_nkeywords(ConfigNodePtr con,int i)
{
    OutputFileNodePtr ofn = NULL;
    if(!con || i <0)
	return -1;
    ofn = get_i_output_file(con->lout_files,i);

    return ofn ? ofn->nkey : -1;
}

char *
LOGR_get_config_logml_file(ConfigNodePtr con,int i)
{
    OutputFileNodePtr ofn = NULL;
    if(!con || i <0)
	return NULL;
    ofn = get_i_output_file(con->lout_files,i);

    return ofn ? ofn->lml_name : NULL;
}

char *
LOGR_get_config_report_prefix(ConfigNodePtr con,int i)
{
    OutputFileNodePtr ofn = NULL;
    if(!con || i <0)
	return NULL;
    ofn = get_i_output_file(con->lout_files,i);

    return ofn ? ofn->rep_prefix : NULL;
}

char *
LOGR_get_config_report_dir(ConfigNodePtr con,int i)
{
    OutputFileNodePtr ofn = NULL;
    if(!con || i <0)
	return NULL;
    ofn = get_i_output_file(con->lout_files,i);

    return ofn ? ofn->rep_dir : NULL;
}

int
LOGR_get_config_num_users(ConfigNodePtr con,int i)
{
    OutputFileNodePtr ofn = NULL;
    if(!con || i <0)
	return NULL;
    ofn = get_i_output_file(con->lout_files,i);

    return ofn ? ofn->nus : -1;
}

int
LOGR_get_config_max_edges(ConfigNodePtr con,int i)
{
    OutputFileNodePtr ofn = NULL;
    if(!con || i <0)
	return NULL;
    ofn = get_i_output_file(con->lout_files,i);

    return ofn ? ofn->max_edges : -1;
}

int
LOGR_get_config_min_edges(ConfigNodePtr con,int i)
{
    OutputFileNodePtr ofn = NULL;
    if(!con || i <0)
	return NULL;
    ofn = get_i_output_file(con->lout_files,i);

    return ofn ? ofn->min_edges : -1;
}

int
LOGR_get_config_num_graphs(ConfigNodePtr con,int i)
{
    OutputFileNodePtr ofn = NULL;
    if(!con || i <0)
	return NULL;
    ofn = get_i_output_file(con->lout_files,i);

    return ofn ? ofn->ngraph : -1;
}



